package com.sgcc.isc.util;


import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.sgcc.common.core.utils.StringUtils;
import com.sgcc.isc.domain.ResultMsg;

/**
 * xml创造器
 * @author xpc
 * @version 2019年11月14日14:53:16
 */
public class XmlCreator {

    /**
     * 组装返回信息
     * @param resultMsg 返回信息实体类
     * @return String xml格式字符串
     */
    public static String resultXml(ResultMsg resultMsg) {
        Document result = DocumentHelper.createDocument();
        result.setXMLEncoding(resultMsg.getXmlEncoding());
        Element rootElement = DocumentHelper.createElement("DBSET");
        rootElement.addAttribute("RESULT", resultMsg.getResult());
        Element r = DocumentHelper.createElement("R");
        Element rtnCode = DocumentHelper.createElement("C");
        rtnCode.addAttribute("N", "rtnCode");
        rtnCode.addText(resultMsg.getRtnCode());
        r.add(rtnCode);
        Element rtnMessage = DocumentHelper.createElement("C");
        rtnMessage.addAttribute("N", "rtnMessage");
        if (StringUtils.isBlank(resultMsg.getRtnMessage())) {
            resultMsg.setRtnMessage("");
        }
        rtnMessage.addText(resultMsg.getRtnMessage());
        r.add(rtnMessage);
        rootElement.add(r);
        result.add(rootElement);
        return result.asXML();
    }

}
